CSS [attribute~=”value”] Selector

The [attribute~=”value”] Selector selector is used to select all the elements whose attribute value is a list of space-separated values, one of which is exactly equal to the specified value. 

Example: 

HTML
<!DOCTYPE html>
<html>
  
<head>
    <title>Attribute selector</title>
    <style>
        [class~="gfg"] {
            color: green;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
        }

        [class~="geeks"] {
            font-size: 17px;
            text-align: center;
            margin-top: 0px;
        }
    </style>
</head>

<body>
    <div class="gfg">w3wiki</div>
    <div Class="geeks">A computer science portal for geeks
    </div>
    <div class="geeks ide">
        w3wiki is coding platform
    </div>
</body>
  
</html>


Output: 

CSS Attribute Selector

The CSS Attribute Selector allows you to target elements based on their specific attributes or attribute values. This selector enhances the precision and efficiency of your CSS, enabling you to apply styles to elements that share common attributes. By leveraging attribute selectors, you can maintain a more organized and scalable CSS codebase. In this article, we will explore the various types of CSS Attribute Selectors, their syntax, and practical examples to demonstrate their usage.

Similar Reads

CSS Attribute Selector Overview

CSS Attribute Selectors enable you to select elements based on the presence or value of their attributes. This selection capability is essential for creating highly specific and reusable styles....

Types of CSS Attribute Selectors

1. CSS [attribute] Selector...

1. CSS [attribute] Selector

The [attribute] Selector is used to select all the elements that have the specified attribute and applies the CSS property to that attribute. For example, the selector [class] will select all the elements with the style attribute....

2. CSS [attribute = “value”] Selector

The [attribute = “value”] Selector selector is used to select all the elements whose attribute has the value exactly the same as the specified value....

3. CSS [attribute~=”value”] Selector

The [attribute~=”value”] Selector selector is used to select all the elements whose attribute value is a list of space-separated values, one of which is exactly equal to the specified value....

4. CSS [attribute|=”value”] Selector

The [attribute|=”value”] Selector This selector is used to select all the elements whose attribute has a hyphen-separated list of values beginning with the specified value. The value has to be a whole word either alone or followed by a hyphen....

5. CSS [attribute^=”value”] Selector

The [attribute^=”value”] Selector This selector is used to select all the elements whose attribute value begins with the specified value. The value doesn’t need to be a whole word....

6. CSS [attribute$=”value”] Selector

The [attribute$=”value”] Selector This selector is used to select all the elements whose attribute value ends with the specified value. The value doesn’t need to be a whole word....

7. CSS [attribute*=”value”] Selector

The [attribute*=”value”] Selector This selector selects all the elements whose attribute value contains the specified value present anywhere. The value doesn’t need to be a whole word....

Conclusion

The CSS Attribute Selector is a good and efficient way to apply styles to HTML elements based on their attributes. By using different types of attribute selectors, you can create more precise and maintainable styles, enhancing both the appearance and functionality of your web pages. Whether you are working with simple attribute presence or complex value matching, these selectors provide the flexibility needed to achieve your design goals....